/*
 * @Author: zi.yang
 * @Date: 2021-10-04 14:45:25
 * @LastEditors: zi.yang
 * @LastEditTime: 2021-10-04 21:49:02
 * @Description: 刷新Token
 * @FilePath: \ziYangPM\src\Utils\refreshToken.js
 */
import store from '@/store'
import router from '@/router'
import Message from '@/components/PmPopup/PmMessage'

export default (requests) => {
  const message = new Message()
  return new Promise((resolve, reject) => {
    store.dispatch('User/FetchRefreshToken').then(() => {
      requests.forEach((request) => request())
      requests.splice(0)
      resolve()
    }).catch(err => {
      reject(err)
      message.error(err.msg)
      // 清除token
      store.commit('User/DEL_TOKEN')
      store.commit('User/DEL_USER_INFO')
      // 跳转登录页面
      setTimeout(() => {
        router.replace({ path: '/auth/sign-in1' })
      }, 1000)
    })
  })
}
